package net.chinatax.rz.zzs;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * 取待认证发票数据；
 * 从防伪税控系统获取结果，写入到本地文件中，供外网客户端获取。
 * @author weitao
 *
 */
public class ZzsRzInSiteInvokeServlet extends HttpServlet {

    public void init()throws ServletException{
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        
         response.setContentType("text/html;charset=utf-8");
         Writer resout=response.getWriter();
         resout.write( "{\"err\":\"" );
         resout.flush();
         resout.close();
    }
 
    
    /**
     * @param bz:1,表示回写认证结果;0,表示取得待认证数据。
     * 		bz为1时所需参数{
     * 			FPDM:**,
     * 			FPHM:**,
     * 			RZJG_DM:**,
     * 			RZJG_MC:**
     * 		}
     * @return json{
     * 		state:1,成功;-1,失败.
     * 		data:[{FPDM:**,FPHM:**,KPRQ:**,....},...]
     * }
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        String bz = request.getParameter("bz");
    	if("1".equals(bz)){
    	     String FPDM = request.getParameter(Tools.FP.FPDM);
    	     String FPHM = request.getParameter(Tools.FP.FPHM);
    	     String RZJG_DM = request.getParameter(Tools.FP.RZJG_DM);
    	     String RZJG_MC = request.getParameter(Tools.FP.RZJG_MC);
    	     Map rzjg = new HashMap();
    	     rzjg.put(Tools.FP.FPDM, FPDM);
    	     rzjg.put(Tools.FP.FPHM, FPHM);
    	     rzjg.put(Tools.FP.RZJG_DM, RZJG_DM);
    	     rzjg.put(Tools.FP.RZJG_MC, RZJG_MC);
    	     String UUID = Tools.UID(FPDM+"_"+FPHM);//发票信息本地存放文件名称
    	     String U_filePath = Tools.pathMap.get(Tools.ZZSOUTPUT).toString() + File.pathSeparator + UUID + ".properties";
    	     Tools.writeAllProperties(U_filePath, rzjg);	
    	     response.setContentType("text/html;charset=utf-8");
	         Writer resout=response.getWriter();
	         resout.write( "{\"state\":\"1\"}" );
	         resout.flush();
	         resout.close();
	         return;
    	}else if("0".equals(bz)){
    		response.setContentType("text/html;charset=utf-8");
            Writer resout=response.getWriter();
            resout.write( "{\"state\":\"1\",data:"+getDRZFPXX()+"}" );
            resout.flush();
            resout.close();
    	}else{
    		response.setContentType("text/html;charset=utf-8");
            Writer resout=response.getWriter();
            resout.write( "{\"state\":\"-1\"}" );
            resout.flush();
            resout.close();
    	}
        
   }
    
    /**
     * 获取认证发票列表，封装成json格式，返回到客户端。
     * @return 发票信息列表json串
     */
    public String getDRZFPXX(){
    	
		List list = new ArrayList();
		
		//获取结果列表
		File fileList[] = new File(Tools.pathMap.get(Tools.ZZSINPUT).toString()).listFiles();
		String[] filePaths = new String[fileList.length];
		for(int i = 0;i < fileList.length; i++){
			filePaths[i] = fileList[i].getAbsolutePath();
			list.add(this.getDRZSingleFP(filePaths[i]));
			fileList[i].delete();//删除文件
		}
		
		JSONArray ja = new JSONArray();
		for(int i=0; i<list.size(); i++) {
			JSONObject jo = new JSONObject();
			jo.putAll((Map)list.get(i));
			ja.add(jo);
		}	
    	return ja.toJSONString();
    }
    
    //获取单条待认证发票数据
    public Map getDRZSingleFP(String fileName){
    	return Tools.getProperties(fileName);
    }

}
